package cn.wolfcode.wolf2w.business.controller;

import cn.wolfcode.wolf2w.business.api.RemoteNoteEsService;
import cn.wolfcode.wolf2w.business.api.domain.NoteEs;

import cn.wolfcode.wolf2w.common.core.constant.SecurityConstants;
import cn.wolfcode.wolf2w.common.core.domain.R;
import cn.wolfcode.wolf2w.common.security.annotation.InnerAuth;
import cn.wolfcode.wolf2w.business.api.domain.Note;
import cn.wolfcode.wolf2w.business.query.NoteQuery;
import cn.wolfcode.wolf2w.business.service.INoteService;
import cn.wolfcode.wolf2w.common.security.annotation.RequiresLogin;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 旅游日记 Controller
 * 
 * @author hnz
 * @date 2025-09-16
 */
@RestController
@RequestMapping("notes")
public class NoteController {
    @Autowired
    private INoteService noteService;
    @Autowired
    private RemoteNoteEsService remoteNoteEsService;


    @PostMapping("star/{noteId}")
    @RequiresLogin
    public R<Map<String, Object>> star(@PathVariable Long noteId) {
        Map<String, Object>map = noteService.star(noteId);
        return R.ok(map);
    }

    @PostMapping("add")
    @RequiresLogin
    public R<?> add(@RequestBody Note note) {
        noteService.add(note);
        return R.ok();
    }

    @PostMapping("collect/{noteId}")
    @RequiresLogin
    public R<Map<String, Object>> collect(@PathVariable Long noteId) {
        Map<String, Object>map = noteService.collect(noteId);
        return R.ok(map);
    }

    @PostMapping("uncollect/{noteId}")
    @RequiresLogin
    public R<Map<String, Object>> uncollect(@PathVariable Long noteId) {
        Map<String, Object>map = noteService.collect(noteId);
        return R.ok(map);
    }

    @GetMapping("viewnnumTop3")
    public R<List<Note>> viewnnumTop3(Long destId) {
        return R.ok(noteService.queryViewnnumTop3(destId));
    }


    //http://localhost:9096/notes/dataInit
    @GetMapping("/dataInit")
    public R<?> dataInit(){
        List<Note> list = noteService.list();
        for (Note note : list) {
            NoteEs es = new NoteEs();
            //同名属性拷贝，把note对象中的数据拷贝到es中
            BeanUtils.copyProperties(note, es);
            remoteNoteEsService.initNoteEs(es, SecurityConstants.INNER);
        }
        return R.ok("游记数据初始化完成");
    }
    /**
     * 旅游日记详情
     */
    @GetMapping("/detail/{id}")
    public R<Note> detail(@PathVariable Long id) {
        Note note = noteService.getNoteById(id);
        return R.ok(note);
    }
    /**
     * 旅游日记 列表
     */
    @GetMapping("/query")
    public R<IPage<Note>> query(NoteQuery qo) {
        IPage<Note> page = noteService.queryPage(qo);
        return R.ok(page);
    }


    /*****************************************对外暴露Fegin接口**********************************************/
    /**
     * Feign 接口
     */

    @PostMapping("/feign/noteHashPersistence")
    public R<?> noteHashPersistence(@RequestHeader(SecurityConstants.FROM_SOURCE) String source){
        noteService.noteHashPersistence();
        return R.ok();
    };


    @GetMapping("/feign/list")
    public R<List<Note>> feignList() {
        return R.ok(noteService.list());
    }
    @InnerAuth
    @GetMapping("/feign/{id}")
    public R<Note> feignGet(@PathVariable Long id) {
        return R.ok(noteService.getById(id));
    }
}
